﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace UI 
{

    public class ActorItem : MonoBehaviour
    {
        public GameObject actorHead;
        public GameObject actorBox;
        public GameObject formationFlag;
        private Dict.DataModel.TableHero hero;
        public Actors actor;//一个英雄头像
        
        public GameObject elementTypeSpr;
        private bool isPress;
        private float pressTime;
        public GameObject heroNameLabel;
        private bool isShowTip = false;
        
       

        public void Init(Actors actor)
        {
           
             this.actor = actor;
             hero = Dict.DictDataManager.Instance.tableHeroDao.GetById(actor.heroid);
             Texture2D t2d = AssetLoad.ResourceManager.Instance.LoadResource("HeroHeader/" + hero.iconName) as Texture2D;
             actorHead.GetComponent<UITexture>().mainTexture = t2d;
             SetState(false);

             //人物属性暂时没有

             heroNameLabel.GetComponent<UILabel>().text = TableTextBlo.GetText(hero.name);
             //if (actor.elementType == Dict.Enum.DictElementType.AN)
             //{
             //    elementTypeSpr.GetComponent<UISprite>().spriteName = "ui_battle_attricon_dark";
             //}
             //else if (actor.elementType == Dict.Enum.DictElementType.FENG)
             //{
             //    elementTypeSpr.GetComponent<UISprite>().spriteName = "ui_battle_attricon_wood";
             //}
             //else if (actor.elementType == Dict.Enum.DictElementType.GUANG)
             //{
             //    elementTypeSpr.GetComponent<UISprite>().spriteName = "ui_battle_attricon_light";
             //}
             //else if (actor.elementType == Dict.Enum.DictElementType.HUO)
             //{
             //    elementTypeSpr.GetComponent<UISprite>().spriteName = "ui_battle_attricon_fire";
             //}
             //else if (actor.elementType == Dict.Enum.DictElementType.SHUI)
             //{
             //    elementTypeSpr.GetComponent<UISprite>().spriteName = "ui_battle_attricon_water";
             //}
        }

        public void HoldOn()
        {

            isShowTip = true;
            //formationPanel.ShowTip(actor);
            FormationPanel._instance.ShowTip(actor);
        }

     

        void Update() 
        {
            if (isPress)
            {
                pressTime += Time.deltaTime;
                if (pressTime >0.5f)
                {
                    HoldOn();
                }
            }
        }
        /// <summary>
        /// 长按,显示Tips
        /// </summary>
        /// <param name="isPressed"></param>
        void OnPress(bool isPressed) 
        {
            if (isPressed) 
            { pressTime = 0; }
            else
            {
                pressTime = 0;
                StartCoroutine(HideTip());
            }
            this.isPress = isPressed;
        }
        /// <summary>
        /// 隐藏Tips
        /// </summary>
        /// <returns></returns>
        IEnumerator HideTip() 
        {
            yield return new WaitForSeconds(0.5f);
            isShowTip = false;
            
            FormationPanel._instance.HideTip();
        }
        /// <summary>
        /// 设置英雄是否被选中
        /// </summary>
        /// <param name="selected"></param>
        public void SetState(bool selected) 
        {
            
            if (actor == null)
            {
                actorBox.transform.localPosition = new Vector3(-10, 0, 0);
                formationFlag.GetComponent<UISprite>().spriteName = "Flag0";
            }
            else if (actor.formationPositionId != 0)
            {
                actorBox.transform.localPosition = new Vector3(-10, 0, 0);
                formationFlag.GetComponent<UISprite>().spriteName = "Flag1";
            }
            else if (selected)
            {
                 actorBox.transform.localPosition = new Vector3(-10, 0, 0);
                formationFlag.GetComponent<UISprite>().spriteName = "Flag0";
            }
            else if (!selected)
            {
                actorBox.transform.localPosition = new Vector3(-52, 0, 0);
                formationFlag.GetComponent<UISprite>().spriteName = "Flag0";
            }
        }

        public void ActorItemOnClick() 
        {
            FormationPanel._instance.RemoveFormation(actor);
            
            if (actor.formationPositionId == 0 && !isShowTip)
            {

                FormationPanel._instance.SetSelectActor(actor);
                actorBox.transform.localPosition = new Vector3(-10, 0, 0);
                
            }
            else if (actor.formationPositionId != 0 && !isShowTip)//如果英雄已经在上阵中了
            {

                FormationPanel._instance.RemoveFormation(actor);
                
            }
            SetState(true);
        }

        public void Clear() 
        {
            actorHead.GetComponent<UITexture>().mainTexture = null;
            AssetLoad.ResourceManager.Instance.UnloadResource("HeroHeader/" + hero.iconName);
        }
        public void DestoryThisGameObject() 
        {
            Destroy(this);
        }
    }
}

